const Koa = require('koa');
const app = new Koa();
const views = require('koa-views');
const json = require('koa-json');
const onerror = require('koa-onerror');
const bodyparser = require('koa-bodyparser');
const logger = require('koa-logger');
const session = require('koa-session2');
const RedisStore = require('./utils/sessionStore');


const CONFIG = require('../../common/config');

const index = require('./routes/index');
const users = require('./routes/users');
const yjxV1 = require('./routes/yjx/v1/index');

// error handler
onerror(app);

// middlewares
app.use(bodyparser({
    enableTypes:['json', 'form', 'text']
}));
app.use(json());
app.use(logger());
app.use(require('koa-static')(__dirname + '/public'));

app.use(views(__dirname + '/views', {
    extension: 'pug'
}));

// logger
app.use(async (ctx, next) => {
    const start = new Date();
    await next();
    const ms = new Date() - start;
    console.log(`${ctx.method} ${ctx.url} - ${ms}ms`)
});

app.use(session({
    key: 'session-key',
    /*
    key: a string for store session id in cookie
    store: a class for custom store (extend {Store}, func: #get(sid), #set(session, opts), #destory(sid))
    maxAge: a number representing the milliseconds from Date.now() for expiry
    expires: a Date object indicating the cookie's expiration date (expires at the end of session by default).
    path: a string indicating the path of the cookie (/ by default).
    domain: a string indicating the domain of the cookie (no default).
    secure: a boolean indicating whether the cookie is only to be sent over HTTPS (false by default for HTTP, true by default for HTTPS).
    httpOnly: a boolean indicating whether the cookie is only to be sent over HTTP(S), and not made available to client JavaScript (true by default).
    signed: a boolean indicating whether the cookie is to be signed (false by default). If this is true, another cookie of the same name with the .sig suffix appended will also be sent, with a 27-byte url-safe base64 SHA1 value representing the hash of cookie-name=cookie-value against the first Keygrip key. This signature key is used to detect tampering the next time a cookie is received.
    overwrite: a boolean indicating whether to overwrite previously set cookies of the same name (false by default). If this is true, all cookies set during the same request with the same name (regardless of path or domain) are filtered out of the Set-Cookie header when setting this cookie.
    * */
    store : new RedisStore({
        host: CONFIG.SJG_MIDDLE_STAGE_REDIS_DOMAIN,
        pass:CONFIG.SJG_MIDDLE_STAGE_REDIS_PASSWORD,
        port:CONFIG.SJG_MIDDLE_STAGE_REDIS_PORT,
        db:CONFIG.SJG_MIDDLE_STAGE_REDIS_DB_NUMBER,
        ttl:CONFIG.SJG_MIDDLE_STAGE_REDIS_TTL,
        prefix:CONFIG.SJG_MIDDLE_STAGE_REDIS_SERVER_PREFIX,
    }),
    // store : new RedisStore({
    //     host:CONFIG.SJG_MIDDLE_STAGE_REDIS_DOMAIN,
    //     pass:CONFIG.SJG_MIDDLE_STAGE_REDIS_PASSWORD,
    //     port:CONFIG.SJG_MIDDLE_STAGE_REDIS_PORT,
    //     db:CONFIG.SJG_MIDDLE_STAGE_REDIS_DB_NUMBER,
    //     ttl:CONFIG.SJG_MIDDLE_STAGE_REDIS_TTL,
    //     prefix:CONFIG.SJG_MIDDLE_STAGE_REDIS_SERVER_PREFIX,
    // })


}), app);


// routes
app.use(index.routes(), index.allowedMethods());
app.use(users.routes(), users.allowedMethods());
app.use(yjxV1.routes(), yjxV1.allowedMethods());

// error-handling
app.on('error', (err, ctx) => {
    console.error('server error', err, ctx)
});

module.exports = app;
